﻿using System;

namespace StringFormats
{
    //自定义字符串格式，继承自IFormattable
    public class Person : IFormattable
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public override string ToString() => FirstName + " " + LastName;

        //给IFormatProvider参数传递null
        public virtual string ToString(string format) => ToString(format, null);

        //传递两个参数，一个是格式字符串参数，一个是IFormatProvider参数
        public string ToString(string format, IFormatProvider formatProvider)
        {
            switch (format)
            {
                case null:
                case "A":
                    return ToString();
                case "F":
                    return FirstName;
                case "L":
                    return LastName;
                default:
                    throw new FormatException($"invalid format string {format}");
            }
        }
    }
}